package com.sfh.dog.ui;

import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Editable;
import android.text.SpannableString;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.pulltorefresh.PullToRefreshBase;
import com.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.pulltorefresh.PullToRefreshListView;
import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.adapter.ReviewAdapter;
import com.sfh.dog.dialog.FaceDalig;
import com.sfh.dog.entity.Dog;
import com.sfh.dog.entity.Review;
import com.sfh.dog.entity.User;
import com.sfh.dog.http.APIConfig;
import com.sfh.dog.http.Const;
import com.sfh.dog.util.FaceUtil;
import com.sfh.dog.util.LogUtil;
import com.sfh.dog.util.PhotoUtil;
import com.sfh.dog.util.SharedPreferencesUtils;
import com.sfh.dog.util.StringUtils;

/**
 * 详情
 * 
 * @version [版本号2014-4-1]
 */
public class DailyActivity extends ADailyActivity
{
    private PullToRefreshListView lvDisplay;
    
    /** 头部视图 */
    private ImageView ivHeadPhoto;
    
    private ImageView ivPicture;
    
    private TextView tvName;
    
    private TextView tvTime;
    
    private TextView tvZan;
    
    private TextView tvContent;
    
    private EditText etReview;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.daily_layout);
        find();
        setListern();
        init();
        initData();
    }
    
    private void find()
    {
        lvDisplay = (PullToRefreshListView)findViewById(R.id.lvDisplay);
        lvDisplay.setScrollingWhileRefreshingEnabled(true);
        View view = LayoutInflater.from(DailyActivity.this).inflate(R.layout.daily_head_layout, null);
        lvDisplay.getRefreshableView().addHeaderView(view);
        findHeadView(view);
    }
    
    /** 头部视图 */
    private void findHeadView(View view)
    {
        etReview  = (EditText)view.findViewById(R.id.etReview);
        ivPicture = (ImageView)view.findViewById(R.id.ivPicture);
        ivHeadPhoto = (ImageView)view.findViewById(R.id.ivPhoto);
        tvName = (TextView)view.findViewById(R.id.tvName);
        tvTime = (TextView)view.findViewById(R.id.tvTime);
        tvZan = (TextView)view.findViewById(R.id.tvZan);
        tvContent = (TextView)view.findViewById(R.id.tvContent);
        ivHeadPhoto.setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View arg0)
            {
                // TODO 进入查看dog
            }
        });
        
        tvZan.setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View arg0)
            {
            }
        });
    }
    
    private void setListern()
    {
        findViewById(R.id.ivLeft).setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                finish();
            }
        });
        lvDisplay.setOnRefreshListener(new OnRefreshListener2<ListView>()
        {
            
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                page = 0;
                getReview();
            }
            
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                page++;
                getReview();
            }
        });
        
        findViewById(R.id.btSend).setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
             
                final User bmobUser = BmobUser.getCurrentUser(DailyActivity.this,User.class);
                if (bmobUser != null)
                {
                    if (StringUtils.isNullOrEmpty(bmobUser.getDogID()))
                    {
                        // 添加dog
                    }else
                    {
                        String content = etReview.getText().toString();
                        if (StringUtils.isNullOrEmpty(bmobUser.getDogID()))
                        {
                            
                        }else
                        {
                            Review review = new Review();
                            review.setDailyID(entity.getObjectId());
                            review.setDogID(bmobUser.getDogID());
                            review.setContent(content);
                            review.save(DailyActivity.this, new SaveListener()
                            {
                                
                                @Override
                                public void onSuccess()
                                {
                                    lvDisplay.doPullRefreshing(500);
                                    
                                }
                                
                                @Override
                                public void onFailure(int arg0, String arg1)
                                {
                                    // TODO Auto-generated method stub
                                    
                                }
                            });
                        }
                    }
                }else
                {
                    //login
                }
            
            }
        });
        
    }
    
    private void initData()
    {
        ImageLoader.getInstance().loadImage(entity.getPicture(), DogApplication.options, new SimpleImageLoadingListener()
        {

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {
                ivPicture.setImageBitmap(loadedImage);
            }
            
        });
        tvContent.setText(entity.getDescription());
        tvZan.setText(entity.getPraiseSize()+"");
        
        BmobQuery<Dog> query = new BmobQuery<Dog>();
        query.addWhereEqualTo("objectId",entity.getDogID());
        query.findObjects(this, new FindListener<Dog>()
        {
            
            @Override
            public void onSuccess(List<Dog> arg0)
            {
                if (arg0 == null || arg0.size() ==0)
                {
                    return;
                }
                
                tvName.setText(arg0.get(0).getName());
                tvName.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(arg0.get(0)
                    .getSex() == 0 ? R.drawable.female : R.drawable.male),
                    null,
                    null,
                    null);
                ImageLoader.getInstance().displayImage(arg0.get(0).getImage(),
                    ivHeadPhoto,
                    DogApplication.options);
            }
            
            @Override
            public void onError(int arg0, String arg1)
            {
                
            }
        });
        
        lvDisplay.doPullRefreshing(500);
    }
    
    @Override
    public void setAdapter()
    {
        mReviewAdapter = new ReviewAdapter(this, new ReviewAdapter.IReview()
        {
            @Override
            public void onClick(String id, boolean isOK)
            {
                
            }
        }, reviews);
        lvDisplay.setAdapter(mReviewAdapter);
    }
    
    @Override
    public void onLoadingComplete()
    {
        lvDisplay.onRefreshComplete();
        
    }
    
}
